<template>
	<view class="content">
		<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			<image class="avatar" :src="avatarUrl"></image>
		</button>
		<view class="content-nickname">
			<input type="nickname" class="weui-input cname" @change="getNickname" placeholder="请输入昵称" />
		</view>



		<view class="content-save">
			<u-button shape="circle" class="myBtn" type="warning" @click="save" text="保存"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		setUserInfo
	} from '@/config/api.js'
	export default {
		data() {
			return {
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
				nickName: ''
			}
		},
		// onChooseAvatar(e) {
		// 	console.log("设置维修昵称和头像111 ==> ", e.detail)
		// 	const avatarUrl = e.detail
		// 	console.log("设置维修昵称和头像111 ==> ", avatarUrl)
		// 	this.avatarUrl = avatarUrl
		// },
		methods: {
			getNickname(e) {
				console.log("昵称  ==> ", e.detail.value)
				this.nickName = e.detail.value
			},
			onChooseAvatar(e) {
				console.log("设置维修昵称和头像222 ==> ", e)
				const {
					avatarUrl
				} = e.detail
				this.avatarUrl = avatarUrl
			},
			save() {
				if (this.nickName == '' || this.nickName.length <= 0) {
					uni.$u.toast("昵称必填")
					return;
				}
				let data = {
					avatar: this.avatarUrl,
					nickName: this.nickName
				}
				console.log("avatarUrl  ==> ", this.avatarUrl)
				console.log("nickName  ==> ", this.nickName)
				setUserInfo(data).then((res) => {
					if (res.code == 200) {
						uni.$u.toast("保存成功")
						uni.$u.route({
							url: '/pages/index/index',
							type: 'reLaunch'
						});
					} else {
						uni.$u.toast(res.msg)
					}
				})
			}
		}
	}
</script>

<style scoped>
	.avatar-wrapper {
		padding: 0;
		width: 56px !important;
		border-radius: 8px;
		margin-top: 40px;
		margin-bottom: 40px;
	}

	.avatar {
		display: block;
		width: 56px;
		height: 56px;
	}

	.content-nickname {
		margin-top: 30px;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
	}

	.cname {
		width: 50%;
		text-align: center;
	}

	.content-save {
		margin-top: 30px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.myBtn {
		width: 80%;
		text-align: center;
	}
</style>